package com.delamer.utils;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;

import java.util.List;
import java.util.Map;

public class ItemUtil {
    public static ItemStack createMenuItem(Plugin plugin, String type, String name) {
        if (type == null || type == "")
            return null;
        Material material = Material.getMaterial(type.toUpperCase());
        if (material == null)
            return null;
        ItemStack itemStack = new ItemStack(material);
        ItemMeta itemMeta = itemStack.getItemMeta();
        if (name != null && name != "")
            itemMeta.setDisplayName(name);
        PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer();
        persistentDataContainer.set(new NamespacedKey(plugin, "PluginMenuItem"), PersistentDataType.STRING, "0");
        itemStack.setItemMeta(itemMeta);
        return itemStack;
    }

    public static ItemStack createMenuItem(Plugin plugin, String type, String name, Map<String, String> map) {
        ItemStack itemStack = createMenuItem(plugin, type, name);
        if (itemStack == null)
            return null;
        ItemMeta itemMeta = itemStack.getItemMeta();
        PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer();
        for (Map.Entry entry : map.entrySet()) {
            try {
                persistentDataContainer.set(new NamespacedKey(plugin, entry.getKey().toString()), PersistentDataType.STRING, entry.getValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        itemStack.setItemMeta(itemMeta);
        return itemStack;
    }

    public static ItemStack addEvent(Plugin plugin, ItemStack itemStack, Map<String, String> map) {
        ItemMeta itemMeta = itemStack.getItemMeta();
        PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer();
        for (Map.Entry entry : map.entrySet()) {
            try {
                persistentDataContainer.set(new NamespacedKey(plugin, entry.getKey().toString()), PersistentDataType.STRING, entry.getValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        itemStack.setItemMeta(itemMeta);
        return itemStack;
    }

    public static ItemStack addLore(Plugin plugin, ItemStack itemStack, List<String> lore) {
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.setLore(lore);
        itemStack.setItemMeta(itemMeta);
        return itemStack;
    }

    public static ItemStack addQuest(Plugin plugin, ItemStack itemStack, Map<String, String> map) {
        ItemMeta itemMeta = itemStack.getItemMeta();
        PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer();
        for (Map.Entry entry : map.entrySet()) {
            try {
                persistentDataContainer.set(new NamespacedKey(plugin, entry.getKey().toString()), PersistentDataType.STRING, entry.getValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        itemStack.setItemMeta(itemMeta);
        return itemStack;
    }
}
